package com.yl.ifanr;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;

import com.yl.ifanr.LoadingUI;
import com.yl.ifanr.db.ItemlistAdapter;
import com.yl.ifanr.db.RssDaoimpl;
import com.yl.ifanr.db.RssItemBean;
import com.yl.ifanr.R;

public class RssIndex extends Activity implements OnItemClickListener{
	public final String TAG = "RssIndex";
	public final String RSS_URL = "http://www.ifanr.com/feed";
	private List<RssItemBean> itemList;
	private ListView listView;
	private RssDaoimpl mDbHelper;
	private RelativeLayout mainLayout;
	private LoadingUI loadingUI;
	private RelativeLayout.LayoutParams pbLP;
	
	public void onCreate(Bundle icicle) {
	    super.onCreate(icicle);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    Log.d(TAG, "onCreate");
	    setContentView(R.layout.rss_index);
	    listView = (ListView) findViewById(R.id.rss_list_item); 
	    listView.setOnItemClickListener(this);
	    
	    mDbHelper = new RssDaoimpl(this);
		
		//显示进度条
		mainLayout = (RelativeLayout)findViewById(R.id.ifan_list_relativelayout);
		loadingUI = new LoadingUI(this, "加载中...");
		pbLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		pbLP.addRule(RelativeLayout.CENTER_IN_PARENT);
		mainLayout.addView(loadingUI, pbLP);
		
	    showListView();
	}
	/**
	 * 显示列表
	 */
	private void showListView()
	{
		Log.d(TAG, "============showListView");
		itemList = mDbHelper.getAllItem();
		ItemlistAdapter itemlistAdapter = new ItemlistAdapter(this,itemList);

		listView.setAdapter(itemlistAdapter);
	    mainLayout.removeView(loadingUI);
	}
	/**
	 * 被点击事件处理程序
	 */
	 public void onItemClick(AdapterView parent, View v, int position, long id)
	 {
		 Intent intent = new Intent(this,ActivityShowDesc.class);
	     Log.d(TAG, "onItemClick111");
	     
	     RssItemBean rssItemBean = (RssItemBean)v.getTag();
	     intent.putExtra("title",rssItemBean.getTitle());
	     intent.putExtra("desc",rssItemBean.getDescription());
	     intent.putExtra("content",rssItemBean.getContent());
	     intent.putExtra("link",rssItemBean.getLink());
	     intent.putExtra("pubdate",rssItemBean.getPubdate());
	     
	     startActivity(intent);
			
	 }
	 
}
